﻿class Arrow extends MovieClip {
	public var dx:Number = 10;
	public var dy:Number = 0;
	
	public var line:MovieClip;
	public var head:MovieClip;
	
	public var mc:MovieClip;
	
	public var ang;
	public var mult = 1;
	
	//set dx,dy to variables of an MC (usually vel.x,vel.y)
	//set line's scale and rotation
	//set head's position and rotation to end of line
	public function update():Void {
		if (mc) {
			_x = mc._x + mc.vel.x;
			_y = mc._y + mc.vel.y;
			
			dx = mc.vel.x;
			dy = mc.vel.y;
		}
		
		line._xscale = getLen() * mult;
		line._rotation = 180/Math.PI*Math.atan2(dy, dx);
		
		head._x = dx * mult;
		head._y = dy * mult;
		head._rotation = 180/Math.PI*Math.atan2(dy, dx);
		
		//ang = getAngle();
	}
	
	public function setLength(dt:Number):Void {
		var ratio = dt / getLen();
		dx *= ratio;
		dy *= ratio;
		
		update();
	}
	
	public function setAngle(a:Number):Void {
		dx = getLen() * Math.cos( Math.PI*a/180 );
		dy = -getLen() * Math.sin( Math.PI*a/180 );
		
		update();
	}
	
	public function getLen():Number {
		return ( Math.sqrt(dx * dx + dy * dy) );
	}
	
	public function getAngle():Number {
		ang = 180/Math.PI*Math.atan2(-dy, dx);
		if ( -180 <= ang && ang < 0 ) {
			ang += 360;
		}
		return ( ang );
	}
	
	public function onEnterFrame() {
		update();
	}
}